<script setup>

import {getCurrentInstance, ref} from "vue";
import {deleteSysNotice, listSysNotice} from "@/api/system/notice";
import {noticeStatusList, noticeTypeList} from "@/enum/enum";
import SysNoticeForm from "@/views/system/notice/SysNoticeForm.vue";
import {convertSortParam, enumToArray} from "@/utils/utils";

const {proxy} = getCurrentInstance();
const sysNoticeList = ref([]);
const total = ref(0);
const sysNoticeFormRef = ref();
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  noticeTitle: null,
  noticeType: null,
  status: null,
  timeArr: null
})
const selectIdIndex = ref(-1);
const selectId = ref();
const noticeTypeOptions = enumToArray(noticeTypeList);
const noticeStatusOptions = enumToArray(noticeStatusList);
const sortFields = ref([]);

const getList = async () => {
  if (sortFields.value.length > 0) {
    queryForm.value.sort = convertSortParam(sortFields.value);
  }
  const {data} = await listSysNotice(queryForm.value)
  total.value = data.total;
  sysNoticeList.value = data.list;
}

const openNotice = (noticeId, type) => {
  sysNoticeFormRef.value.open(noticeId, type)
}

const deleteNotice = async (noticeId) => {
  proxy.$modal.confirm('确定删除该记录吗？').then(async () => {
    await deleteSysNotice(noticeId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleHeaderClick = (column) => {
  if (!column) return;
  if (column.sortable !== "custom") {
    return;
  }
  if (!column.multiOrder) {
    column.multiOrder = "descending";
  } else if (column.multiOrder === "descending") {
    column.multiOrder = "ascending";
  } else {
    column.multiOrder = "";
  }
  handleOrderChange(column.property, column.multiOrder);
}

const handleOrderChange = (column, type) => {
  let sortType = "";
  switch (type) {
    case "descending":
      sortType = "desc";
      break;
    case "ascending":
      sortType = "asc";
      break;
    case "":
      sortType = "";
      break;
  }
  let index = sortFields.value.findIndex(e => e.orderColumn === column);
  if (index !== -1) {
    if(sortType === ""){
      sortFields.value.splice(index, 1);
    }else{
      sortFields.value[index].orderType = sortType;
    }
  } else {
    sortFields.value.push({
      orderColumn: column,
      orderType: sortType
    });
  }
  getList();
}

const handleSort = ({column}) => {
  if (column.sortable !== "custom") {
    return;
  }
  if (!column.multiOrder) {
    column.multiOrder = "descending";
  } else if (column.multiOrder === "descending") {
    column.multiOrder = "ascending";
  } else {
    column.multiOrder = "";
  }
  handleOrderChange(column.property, column.multiOrder);
}

const handleHeaderClass = ({column}) => {
  if (!column) return;
  column.order = column.multiOrder;
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (index, row) => {
  selectIdIndex.value = index;
  selectId.value = row.noticeId
}

const handleUpdate = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  openNotice(selectId.value, 'update');
}

const handleDelete = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  deleteNotice(selectId.value)
}
getList();
</script>

<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryFormRef" :inline="true" class="demo-form-inline">
      <el-form-item label="公告标题" prop="noticeTitle">
        <el-input v-model="queryForm.noticeTitle" placeholder="请输入公告标题"/>
      </el-form-item>
      <el-form-item label="公告类型" prop="noticeType">
        <el-select v-model="queryForm.noticeType" placeholder="请选择公告类型">
          <el-option v-for="(item, index) in noticeTypeOptions" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公告状态" prop="status">
        <el-select v-model="queryForm.status" placeholder="请选择公告状态">
          <el-option v-for="(item, index) in noticeStatusOptions" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="timeArr">
        <el-date-picker
            v-model="queryForm.timeArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </el-button>
        <el-button plain @click="handleReset">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <div class="flex mb-5">
      <el-button size="default" type="primary" plain class="flex items-center" @click="openNotice(null, 'create')" v-hasPermission="['system:notice:add']">
        <icon icon="ep-plus" size="14px" color="unset" class="mr-1"/>
        新增
      </el-button>

      <el-button size="default" type="success" plain class="flex items-center" @click="handleUpdate" v-hasPermission="['system:notice:edit']">
        <icon icon="ep-edit" size="14px" color="unset" class="mr-1"/>
        编辑
      </el-button>

      <el-button size="default" type="danger" plain class="flex items-center" @click="handleDelete" v-hasPermission="['system:notice:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </el-button>
    </div>

    <el-table :data="sysNoticeList" :border="true" size="large" highlight-current-row @sort-change="handleSort"
              :header-cell-class-name="handleHeaderClass" @header-click="handleHeaderClick">
      <el-table-column width="55" :draggable="false">
        <template #default="scope">
          <el-radio v-model="selectIdIndex" :value="scope.$index"
                    @change="handleSelectionChange(scope.$index,scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column prop="noticeId" label="主键ID" sortable="custom"/>
      <el-table-column prop="noticeTitle" label="公告标题"/>
      <el-table-column prop="noticeType" label="公告类型">
        <template #default="scope">
          <el-tag :type="noticeTypeList[scope.row.noticeType].type">{{ noticeTypeList[scope.row.noticeType].label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="公告状态">
        <template #default="scope">
          <el-tag :type="noticeStatusList[scope.row.status].type">{{ noticeStatusList[scope.row.status].label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"/>
      <el-table-column prop="createTime" label="添加时间" sortable="custom"/>
      <el-table-column fixed="right" label="操作" width="200px">
        <template #default="scope">
          <el-button class="ml-2" type="primary" link @click="openNotice(scope.row.noticeId,'update')" v-hasPermission="['system:notice:edit']">编辑
          </el-button>
          <el-button class="ml-2" type="danger" link @click="deleteNotice(scope.row.noticeId)" v-hasPermission="['system:notice:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

    <sys-notice-form ref="sysNoticeFormRef" @success="getList"/>
  </div>
</template>

<style scoped lang="scss">

</style>
